package prototipo.server.logic.item;

import prototipo.server.logic.exception.ExceptionLogic;

/**
 * Class to represent a item.
 * 
 * @author pablo
 * @since 08/11/2009
 * 
 */
public abstract class Item implements ItemIF {

	private int id;
	private String name;
	private String description;
	private int amount;
	private String measure;

	// private boolean isDeficit;

	/**
	 * @param name
	 * @param description
	 * @param amount
	 * @param unidadeMedida
	 * @throws ExceptionLogic
	 */
	public Item(String name, String description, int amount,
			String unidadeMedida) throws ExceptionLogic {
		super();
		this.setName(name);
		this.setDescription(description);
		this.setAmount(amount);
		this.setmeasure(unidadeMedida);
	}

	public void increase(int quant) {
		this.amount += quant;
	}

	public void decrement(int quant) {
		this.amount -= quant;
		if (this.amount < 1) {
			// TODO avisar a quantidade negativa de item.
		}
	}

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 * @throws ExceptionLogic
	 */
	public void setName(String name) throws ExceptionLogic {
		if (name == null || name.equals(""))
			throw new ExceptionLogic("Nome vazio.", new Throwable(
					"String name is null."));
		this.name = name;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the amount
	 */
	public int getAmount() {
		return amount;
	}

	/**
	 * @param amount
	 *            the amount to set
	 * @throws ExceptionLogic
	 */
	public void setAmount(int amount) throws ExceptionLogic {
		if (amount < 0);
			//TODO notificar o defict do item.
		this.amount = amount;
	}

	/**
	 * @return the measure
	 */
	public String getMeasure() {
		return measure;
	}

	/**
	 * @param measure
	 *            the measure to set
	 * @throws ExceptionLogic
	 */
	public void setmeasure(String measure) throws ExceptionLogic {
		if (measure == null || measure.equals(""))
			throw new ExceptionLogic("Medida vazia.", new Throwable(
					"String measure is null."));
		this.measure = measure;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return String.format("(%d) %s %d(%s)", getId(), getName(), getAmount(),
				getMeasure());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public final int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public final boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Item))
			return false;
		Item other = (Item) obj;
		if (id != other.id)
			return false;
		return true;
	}

}